package org.itboys.mobile.controller.mobile;

import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiParam;
import org.itboys.commons.CommonConstants;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.mobile.dto.dynamic.SundayDynamicDto;
import org.itboys.mobile.service.mongo.front.dynamic.SundayFrontDynamicService;
import org.itboys.mobile.util.AccountIdHoldUtils;
import org.itboys.mobile.util.ResponseMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 作者：zhangyang
 * 时间：2017年12月18日
 * 描述：圣代_动态_controller_mobile
 */
@Controller
@RequestMapping("/sunday/mobile/dynamic")
public class SundayMobileDynamicController extends BaseController{

    @Autowired
    private SundayFrontDynamicService dynamicService;



    @RequestMapping(value="/select", method= {RequestMethod.GET,RequestMethod.POST})
    @ApiOperation(value="【手机端】分页获取动态数据:本地爱购/达人试用/特别关注")
    public void select(HttpServletRequest request,
                        HttpServletResponse response,
                        @ApiParam(required=true, name="type", value="类别:2-达人试用,3-本地爱购,4-特别关注.【参与签名】")@RequestParam(value = "type", required = true)Integer type,
                        @ApiParam(required = true, name = "pageSize", value = "分页参数。每页数量。默认20") @RequestParam(value = "pageSize", required = true) String pageSize,
                        @ApiParam(required = true, name = "pageNumber", value = "分页参数。第几页。默认1") @RequestParam(value = "pageNumber", required = true) String pageNumber,
                        @ApiParam(required = false, name = "sort", value = "排序参数。字段名称。默认为空") @RequestParam(value = "sort", required = false) String sort,
                        @ApiParam(required = false, name = "order", value = "排序参数。。默认为空。ASC正序/DESC倒序") @RequestParam(value = "order", required = false) String order,
                        @ApiParam(value = "随机数。【参与签名】")@RequestParam(value = "nonce", required = true) String nonce,
                        @ApiParam(value = "签名")@RequestParam(value = "sign", required = true) String sign){
        try{
            Long memberId =AccountIdHoldUtils.getMemberId();
            List<SundayDynamicDto> dynamics = dynamicService.selectDynamic(request,memberId, type);
            ResponseMessage.success(dynamics);
        }catch (Exception e){
            String msg = CommonConstants.ERROMSG;
            Integer code= CommonConstants.FAIL_CODE;
            //自定义异常（签名错误）
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
                //token已过期
            }else  if(e.getClass().getSimpleName().equalsIgnoreCase("TokenException")){
                code= CommonConstants.TOKEN_CODE;
                msg= CommonConstants.TOKENMSG;
            }else{
                logger.error("/sunday/mobile/dynamic/operate----e="+e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(code,msg);
        }
    }
    @RequestMapping(value="/selectByPerson", method= {RequestMethod.GET,RequestMethod.POST})
    @ApiOperation(value="【手机端】不分页获取个人动态数据（含个人信息）")
    public void selectByPerson(HttpServletRequest request,
                       HttpServletResponse response,
                       @ApiParam(required=true, name="personId", value="用户ID。需要查看动态的用户ID【参与签名】")@RequestParam(value = "personId", required = true)Long personId,
                       @ApiParam(value = "随机数。【参与签名】")@RequestParam(value = "nonce", required = true) String nonce,
                       @ApiParam(value = "签名")@RequestParam(value = "sign", required = true) String sign){
        try{
            Long memberId =AccountIdHoldUtils.getMemberId();
            Map<String,Object> result = dynamicService.selectDynamicByPerson(request,memberId,personId);
            ResponseMessage.success(result);
        }catch (Exception e){
            String msg = CommonConstants.ERROMSG;
            Integer code= CommonConstants.FAIL_CODE;
            //自定义异常（签名错误）
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
                //token已过期
            }else  if(e.getClass().getSimpleName().equalsIgnoreCase("TokenException")){
                code= CommonConstants.TOKEN_CODE;
                msg= CommonConstants.TOKENMSG;
            }else{
                logger.error("/sunday/mobile/dynamic/selectByPerson----e="+e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(code,msg);
        }
    }
    @RequestMapping(value="/detail", method= {RequestMethod.GET,RequestMethod.POST})
    @ApiOperation(value="【手机端】动态详情（含不分页的评论信息）")
    public void detail(HttpServletRequest request,
                               HttpServletResponse response,
                               @ApiParam(required=true, name="dynamicId", value="用户ID。动态ID【参与签名】")@RequestParam(value = "dynamicId", required = true)Long dynamicId,
                               @ApiParam(value = "随机数。【参与签名】")@RequestParam(value = "nonce", required = true) String nonce,
                               @ApiParam(value = "签名")@RequestParam(value = "sign", required = true) String sign){
        try{
            Long memberId =AccountIdHoldUtils.getMemberId();
            SundayDynamicDto dynamic = dynamicService.detail(dynamicId,memberId);
            ResponseMessage.success(dynamic);
        }catch (Exception e){
            String msg = CommonConstants.ERROMSG;
            Integer code= CommonConstants.FAIL_CODE;
            //自定义异常（签名错误）
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
                //token已过期
            }else  if(e.getClass().getSimpleName().equalsIgnoreCase("TokenException")){
                code= CommonConstants.TOKEN_CODE;
                msg= CommonConstants.TOKENMSG;
            }else{
                logger.error("/sunday/mobile/dynamic/detail----e="+e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(code,msg);
        }
    }


    @RequestMapping(value="/operate", method= {RequestMethod.GET,RequestMethod.POST})
    @ApiOperation(value="【手机端】点赞或评论")
    public void operate(HttpServletRequest request,
                        HttpServletResponse response,
                         @ApiParam(required=true, name="dynamicId", value="动态ID")@RequestParam(value = "dynamicId", required = true)Long dynamicId,
                         @ApiParam(required=true, name="type", value="类型。1点赞，2评论")@RequestParam(value = "type", required = true)Integer type,
                         @ApiParam(required=false, name="content", value="评论内容")@RequestParam(value = "content", required = false)String content){
        try{
            Long memberId =AccountIdHoldUtils.getMemberId();
            dynamicService.operate(dynamicId,memberId, type, content);
            ResponseMessage.success();
        }catch (Exception e){
            String msg = CommonConstants.ERROMSG;
            Integer code= CommonConstants.FAIL_CODE;
            //自定义异常（签名错误）
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
                //token已过期
            }else  if(e.getClass().getSimpleName().equalsIgnoreCase("TokenException")){
                code= CommonConstants.TOKEN_CODE;
                msg= CommonConstants.TOKENMSG;
            }else{
                logger.error("/sunday/mobile/dynamic/operate----e="+e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(code,msg);
        }
    }

}
